<!-- 正在会议 -->
<template>
  <div class='on-meeting'>
    <div class="video-window">
      <video ref="meVideo"
             autoplay
             playsInline
             muted
             class="video-style" />
    </div>
    <peer v-for="(item,index) in peers"
          :key="index"
          :audio-track="item.audioTrack"
          :video-track="item.videoTrack"></peer>
  </div>
</template>

<script>
import Peer from './Peer.vue';
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
export default {
  //import引入的组件需要注入到对象中才能使用
  components: { Peer },
  data () {
    //这里存放数据
    return {
      // 房间客户端
      roomClient: null,
      peers: []
    }
  },
  //方法集合
  methods: {

  },
  //监听属性 类似于data概念
  computed: {
    //计算两个属性
    mePeer () {
      return this.$store.state.room.me;
    }
  },
  //监控data中的数据变化
  watch: {
    // 监听数据的变化
    mePeer: {
      deep: true,
      handler: function (newValue, oldValue) {

        // 监听到数据有变化就进行处理
        const meVideoProducer = newValue.producers.find((producer) => producer.track.kind === 'video');
        if (meVideoProducer) {
          try {
            const meVideoTrack = meVideoProducer.track;
            if (meVideoTrack) {
              let videoStream = new MediaStream();
              videoStream.addTrack(meVideoTrack);
              this.$refs.meVideo.srcObject = videoStream;
              this.$refs.meVideo.play().catch((error) => console.error('本地视频播放失败:%o', error));
            } else {
              this.$refs.meVideo.srcObject = null;
            }
          } catch (e) {
            console.log(e)
          }
        }

        console.log(newValue.consumerPeers);
        // 获取该端的消费者peer
        if (newValue.consumerPeers && newValue.consumerPeers.length > 0) {
          this.peers = [];
          newValue.consumerPeers.forEach((peer) => {
            if (peer.consumers && peer.consumers.length > 0) {
              const audioConsumer = peer.consumers.find((consumer) => consumer.track.kind === 'audio');
              const videoConsumer = peer.consumers.find((consumer) => consumer.track.kind === 'video');
              this.peers.push({
                audioTrack: audioConsumer ? audioConsumer.track : null,
                videoTrack: videoConsumer ? videoConsumer.track : null
              });
            }
            console.log('peers 的个数%o', this.peers);

          });
        }





      }
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created () {
    let roomClient = this.$store.state.roomClient;
    if (roomClient) {
      roomClient.join();
      this.roomClient = roomClient;
    }

  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {

  },
  //生命周期 - 创建之前
  beforeCreate () {

  },
  //生命周期 - 挂载之前
  beforeMount () {

  },
  //生命周期 - 更新之前
  beforeUpdate () {

  },
  //生命周期 - 更新之后
  updated () {

  },
  //生命周期 - 销毁之前
  beforeUnmount () {

  },
  //生命周期 - 销毁完成
  unmounted () {

  },
  //如果页面有keep-alive缓存功能，这个函数会触发
  activated () {

  }
}
</script>
<style lang='less' scoped>
//@import url(); 引入公共css类
.on-meeting {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: flex-start;
  align-items: flex-start;
  position: relative;
  background: whitesmoke;
  .video-window {
    width: 3.2rem;
    height: 2rem;
    margin: 1px 2px;
    .video-style {
      width: 3.2rem;
      height: 2rem;
    }
  }
}
</style>